<?php


namespace Packages\User\Controllers\Admin;


use App\Http\Controllers\Controller;
use Packages\User\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected UserService $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * 获取全部
     * @param Request $request
     * @return JsonResponse
     */
    public function all(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'select' => 'string|max:128',
            'app_id' => 'string|size:32',
            'type' => 'integer|between:1,3',
            'limit' => 'integer',
        ]);

        $result = $this->userService->all($params);

        return $this->returnResult($result);
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'string|size:32',
            'app_id' => 'string|size:32',
            'gender' => 'integer|between:0,2',
            'status' => 'integer|between:1,10',
            'username' => 'string|max:32',
            'nick_name' => 'string|max:32',
            'bind_email' => 'string|max:128',
            'bind_phone' => 'integer|regex:/^1[0-9]{10}$/',
            'type' => 'integer|between:1,3',
            'current' => 'integer|between:1,10000',
            'pageSize' => 'integer|between:1,1000',
        ]);

        $result = $this->userService->list($params);

        return $this->returnResult($result);
    }

    /**
     * 注册用户
     * @param Request $request
     * @return JsonResponse
     */
    public function register(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'nick_name' => 'required|string|min:1|max:32',
            'username' => 'required|string|min:4|max:32',
            'avatar' => 'required|string|max:256',
            'password' => 'required|string|min:6|max:32',
            'meta' => 'required|array',
            'meta.role_id' => 'required|string:size:32',
            'gender' => 'integer|between:0,2',
            'bind_phone' => 'regex:/^1[0-9]{10}$/',
        ]);

        $params['type'] = 2; // 管理员
        $params['gender'] = $params['gender'] ?? 0; // 性别默认未知

        $res = $this->userService->register($params);

        return $this->returnRes($res);
    }

    /**
     * 用户登录
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            // 'method' => 'required|string|max:16',
            'username' => 'required|string|min:4|max:32',
            'password' => 'required|string|min:6|max:32',
            // 'bind_email' => 'string|min:2|max:128',
            // 'bind_phone' => 'regex:/^1[0-9]{10}$/',
            'verifyCode' => 'required|string|size:4',
            'number' => 'required|integer',
        ]);

        $params['last_ip'] = $request->getClientIp();
        $params['method'] = 'username'; // 账号密码登录

        // 验证码效验
        $verify = app('captchaService')->verifyCode($params);
        if (!$verify['status']) return $this->returnRes($verify);

        $res = $this->userService->login($params);

        return $this->returnRes($res);
    }

    /**
     * 退出登录
     * @param Request $request
     * @return JsonResponse
     */
    public function loginOut(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
        ]);

        $res = $this->userService->loginOut($params['guid']);

        return $this->returnRes($res);
    }

    /**
     * 更新用户信息
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
            'nick_name' => 'required|string|min:1|max:32',
            'avatar' => 'required|string|max:256',
            'gender' => 'integer|between:0,2',
            'birthday' => 'integer',
        ]);

        $res = $this->userService->update($params['guid'], $params);

        return $this->returnResult($res, '更新失败!');
    }

    /**
     * 更新绑定信息
     * @param Request $request
     * @return JsonResponse
     */
    public function loginUpdate(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
            'username' => 'string|min:4|max:32',
            'password' => 'string|min:6|max:32',
            'bind_email' => 'string|min:6|max:128',
            'bind_phone' => 'regex:/^1[0-9]{10}$/',
        ]);

        $res = $this->userService->loginUpdate($params['guid'], $params);

        return $this->returnRes($res);
    }

    /**
     * 更新登录状态
     * @param Request $request
     * @return JsonResponse
     */
    public function statusUpdate(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
            'status' => 'required|between:1,9',
        ]);

        // 修改状态
        $res = $this->userService->statusUpdate($params['guid'], $params['status']);

        // 登录退出
        $this->userService->loginOut($params['guid']);

        return $this->returnResult($res, '更新失败!');
    }

    /**
     * 重置密码
     * @param Request $request
     * @return JsonResponse
     */
    public function resetPass(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|max:32',
            'password' => 'required|string|min:6|max:20',
            'passwords' => 'required|string|min:6|max:20',
        ]);

        if ($params['passwords'] != $params['password']) {
            return $this->returnErrorResponse('两次输入不一致');
        }

        $res = $this->userService->resetPass($params['guid'], $params);

        return $this->returnRes($res);
    }

    /**
     * 用户详情
     * @param Request $request
     * @return JsonResponse
     */
    public function userInfo(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|max:32',
        ]);

        // 获取用户信息
        $user = $this->userService->userInfo($params['guid']);

        if ($user) {
            if (@$user->meta->role_id) {
                // 获取角色权限信息
                $user->auth = app('authService')->roleAuthAllInfo($user->meta->role_id);
            } else {
                $user->auth = [];
            }
        }

        return $this->returnResult($user, '用户不存在!');
    }
}
